package br.pucrs.lognplay.faces.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.pucrs.lognplay.entity.Client;
import br.pucrs.lognplay.entity.User;
import br.pucrs.lognplay.entity.User.Role;
import br.pucrs.lognplay.service.ClientManagementService;
import br.pucrs.lognplay.service.UserManagementService;

@ManagedBean
@ViewScoped
public class UserManagementBean extends Bean {

	private User user;

	private List<Client> clients;

	@EJB
	private UserManagementService userManagementService;

	@EJB
	private ClientManagementService clientManagementService;

	public User getUser() {
		try {
			if (user == null) {
				String userId = getParameterValue("userId");

				if (userId == null) {
					Role loggedUserRole = getLoggedUser().getRole();

					if (loggedUserRole.equals(Role.MASTER_USER)) {
						user = new User(UUID.randomUUID());
					} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
						user = new User(UUID.randomUUID());
						user.setClient(getLoggedUser().getClient());
					} else if (loggedUserRole.equals(Role.CLIENT_USER)) {
						user = getLoggedUser();
					}
				} else {
					user = userManagementService.getUserById(userId);
				}
			}

			return user;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean isRoleRendered() {
		Role loggedUserRole = getLoggedUser().getRole();

		if (loggedUserRole.equals(Role.MASTER_USER) || loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			return true;
		} else {
			return false;
		}
	}

	public List<Role> getRoles() {
		List<Role> roles = new ArrayList<>();
		Role loggedUserRole = getLoggedUser().getRole();

		if (loggedUserRole.equals(Role.MASTER_USER)) {
			roles.add(Role.MASTER_USER);
			roles.add(Role.CLIENT_MASTER_USER);
			roles.add(Role.CLIENT_USER);
		} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
			roles.add(Role.CLIENT_MASTER_USER);
			roles.add(Role.CLIENT_USER);
		}

		return roles;
	}

	public void handleRoleChange() {
		Role userRole = user.getRole();

		if (userRole.equals(Role.MASTER_USER)) {
			user.setClient(null);
		}
	}

	public boolean isClientRendered() {
		Role loggedUserRole = getLoggedUser().getRole();
		Role userRole = user.getRole();

		if (loggedUserRole.equals(Role.MASTER_USER) && userRole != null && !userRole.equals(Role.MASTER_USER)) {
			return true;
		} else {
			return false;
		}
	}

	public List<Client> getClients() {
		try {
			if (clients == null) {
				clients = clientManagementService.listAllClients();
			}

			return clients;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public String save() {
		try {
			userManagementService.mergeUser(user);
			addInformationMessage("The changes have been saved", null);
			return null;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}
}
